package cn.enilu.flash.service.dashboard;

import cn.enilu.flash.bean.enumeration.shop.OrderEnum;
import cn.enilu.flash.bean.enumeration.shop.PayStatusEnum;
import cn.enilu.flash.service.shop.CartService;
import cn.enilu.flash.service.shop.OrderService;
import cn.enilu.flash.service.shop.ShopUserService;
import cn.enilu.flash.utils.DateUtil;
import cn.enilu.flash.utils.Maps;
import org.joda.time.DateTime;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：enilu
 * @date ：Created in 1/7/2020 2:25 PM
 */
@Service
public class DashboardService {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ShopUserService shopUserService;
    @Autowired
    private CartService cartService;

    /**
     * 页面汇总数据线上
     *
     * @return
     */
    public Map getDashboardData() {
        long orderCount = orderService.count();
        long userCount = shopUserService.count();
        long cartCount = cartService.count();

        Map orderSumPrice = orderService.getMapBySql("select sum(real_price) as realPrice from t_shop_order where status<>" + OrderEnum.OrderStatusEnum.UN_PAY.getId());
        Map result = Maps.newHashMap(
                "orderCount", orderCount,
                "userCount", userCount,
                "cartCount", cartCount,
                "orderSumPrice", orderSumPrice.getOrDefault("realPrice", null) != null ? (Double.valueOf(orderSumPrice.get("realPrice").toString()) / 100) : "0"
        );
        return result;
    }
    public JSONObject getDashboardOrder() {
        DateTime dateTime = new DateTime();
//        获取前7天的订单数据
        DateTime days_7 = dateTime.plusDays(-6);
        List<Map> list = orderService.queryBySql("SELECT pay_status,count(pay_status) as count,SUBSTR(create_time,1,10) as create_time  " +
                "FROM t_shop_order where SUBSTR(create_time,1,10) BETWEEN  '" + days_7.toString("yyyy-MM-dd")
                + "'  and '" + dateTime.toString("yyyy-MM-dd") +
                "'GROUP BY pay_status, SUBSTR(create_time,1,10)");
        JSONObject jsonAll = new JSONObject();
        //        组装获取JSON
        JSONArray jsonDate = new JSONArray();
        for (int i = 0; i < 7; i++) {
            jsonDate.put(days_7.plusDays(i).toString(DateUtil.DATE_FMT));
        }
        jsonAll.put("dateTime", jsonDate);

//        填充数据
//        添加条表头
        JSONArray titel = new JSONArray();
        Map<String, int[]> maps = new HashMap<>();
        for (PayStatusEnum val : PayStatusEnum.values()) {
            maps.put(val.getName(), new int[7]);
            titel.put(val.getName());
        }
        for (Map map : list) {
            Integer pay_status = (Integer) map.getOrDefault("pay_status", null);
            String name_pay = PayStatusEnum.getValueOf(pay_status.intValue()).getName();
//         获取前台页面的数据集合
            int[] counts = maps.getOrDefault(name_pay, new int[7]);
            //获取数据返回的时间
            String create_time = (String) map.getOrDefault("create_time", null);
//            h获取相差天数
            int index = DateUtil.diffOfDay(DateTime.parse(create_time).toDate(), dateTime.toDate());
            BigInteger index_COUNT = (BigInteger) map.getOrDefault("count", null);
            if (index_COUNT != null && index_COUNT.intValue() > 0) {
                counts[counts.length - index - 1] = index_COUNT.intValue();
            }
            maps.put(name_pay, counts);
        }
        JSONArray array = new JSONArray();
        for (String str : maps.keySet()) {
            JSONObject json = new JSONObject();
            json.put("name", str);
            json.put("type", "line");
//            json.put("stack", "总量");
            int[] value = maps.get(str);
            JSONArray array1 = new JSONArray();
            for (int i : value) {
                array1.put(i);
            }
            json.put("data", array1);
            array.put(json);
        }
        jsonAll.put("titel", titel);
        jsonAll.put("seriesData", array);
        return jsonAll;
    }
}
